<template>
  <div class="login-page">
    <el-card class="box-card">
      <template #header>
        <span>黑马面经运营后台</span>
      </template>

      <el-form ref="loginForm" :model="form" :rules="rules" >
         <el-form-item prop="username" label="用户名:">

          <el-input
          placeholder="请输入用户名"
          v-model.trim="form.username"
          type="text"
          ></el-input>
         </el-form-item>
          <el-form-item prop="password" label="密码:">
          <el-input
          placeholder="请输入密码"
          v-model.trim="form.password"
          type="password"
          ></el-input>
         </el-form-item>
         <el-button type="primary" @click="onSubmit">登录</el-button>
         <el-button @click="reset">重置</el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { login } from '@/api/user'
export default {
  name: 'login-page',

  data () {
    return {
      form: {
        username: '',
        password: ''

      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^\w{5,15}$/, message: '请输入5~15个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\w{5,15}$/, message: '请输入5~15个字符', trigger: 'blur' }
        ]

      }
    }
  },
  methods: {
    onSubmit () {
      this.$refs.loginForm.validate(async (valid) => {
        if (!valid) return
        const res = await login(this.form)
        // console.log(res)
        this.$store.commit('user/setVuexToken', res.data.token)
        this.$message.success('登录成功')

        this.$router.push('/')
      })
    },
    reset () {
      this.$refs.loginForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.login-page{
  background: url('../../assets/login-bg.svg') no-repeat center center;
  background-size: cover;
 display: flex;
  .box-card{
  width: 420px;
   margin: 100px auto;
 ::v-deep .el-card__header{
    background-color: #727cf5;
    color: #fff;
    text-align: center;
    font-size: 18px;
 }
  p{
    margin: 0;
    padding: 0;
  }
}
}

</style>
